package com.qi.controller;

import cn.dev33.satoken.annotation.SaCheckRole;
import com.qi.common.RoleKey;
import com.qi.pojo.entity.Type;
import com.qi.pojo.vo.AjaxResult;
import com.qi.services.ITypeService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/admin/type")
@Tag(name = "社团类型管理")
public class TypeController {

    @Resource
    private ITypeService typeService;

    @SaCheckRole(RoleKey.超级管理员)
    @Operation(summary = "新增社团类型")
    @PostMapping("/add/{name}")
    public AjaxResult add(@PathVariable("name") String name) {
        typeService.add(name);
        return AjaxResult.ok("社团类型新增成功");
    }

    @SaCheckRole(RoleKey.超级管理员)
    @Operation(summary = "编辑社团类型")
    @PostMapping("/edit")
    public AjaxResult edit(@RequestBody Type type) {
        boolean updated = typeService.updateById(type);
        if (updated) {
            return AjaxResult.ok("社团类型编辑成功");
        }
        return AjaxResult.error("社团类型编辑失败");
    }

    @SaCheckRole(RoleKey.超级管理员)
    @Operation(summary = "删除社团类型")
    @GetMapping("/remove/{typeId}")
    public AjaxResult remove(@PathVariable("typeId") Long typeId) {
        boolean removed = typeService.removeById(typeId);
        if (removed) {
            return AjaxResult.ok("社团类型删除成功");
        }
        return AjaxResult.error("社团类型删除失败");
    }

    @SaCheckRole(RoleKey.超级管理员)
    @Operation(summary = "查询社团类型列表")
    @GetMapping("/list")
    public AjaxResult list() {
        List<Type> typeList = typeService.list();
        return AjaxResult.success(typeList);
    }
}